<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="申请类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择申请类型" clearable>
          <el-option label="学生" value="0" />
          <el-option label="教师" value="1" />
          <el-option label="团队" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="保存未提交" :value="0" />
          <el-option label="未审核" :value="1" />
          <el-option label="审核通过" :value="2" />
          <el-option label="审核不通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="applicationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0" type="success">学生</el-tag>
          <el-tag v-else-if="scope.row.type === 1" type="warning">教师</el-tag>
          <el-tag v-else type="info">团队</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="itemName" />
      <el-table-column label="负责人" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="申请时间" align="center" prop="applyDate" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="info">保存未提交</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="warning">未审核</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="danger">审核不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.status === 1"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <!-- 基本信息 -->
        <el-card>
          <div slot="header" class="header">基本信息</div>
          <el-form-item label="申请时间">{{ form.applyDate }}</el-form-item>
          <el-form-item label="项目名称">{{ form.itemName }}</el-form-item>
          <el-form-item label="负责人">{{ form.contactName }}</el-form-item>
          <el-form-item label="联系电话">{{ form.contactPhone }}</el-form-item>
          <el-form-item label="注册资本">{{ form.registerMoney }}元</el-form-item>
          <el-form-item label="人数">{{ form.numbers }}人</el-form-item>
          <el-form-item label="经营范围">{{ form.businessScope }}</el-form-item>
        </el-card>

        <!-- 团队成员 -->
        <el-card>
          <div slot="header" class="header">团队成员</div>
          <el-table :data="form.members" style="width: 100%">
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="job" label="职务" />
            <el-table-column prop="major" label="专业" />
            <el-table-column prop="department" label="院系" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="qq" label="QQ" />
            <el-table-column prop="wechat" label="微信" />
          </el-table>
        </el-card>

        <!-- 指导教师 -->
        <el-card>
          <div slot="header" class="header">指导教师</div>
          <el-table :data="form.teachers" style="width: 100%">
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="job" label="职务" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="email" label="邮箱" />
          </el-table>
        </el-card>

        <!-- 项目介绍 -->
        <el-card>
          <div slot="header" class="header">项目介绍</div>
          <el-form-item label="项目简介">{{ form.projectIntroduction }}</el-form-item>
          <el-form-item label="需要帮助">{{ form.helpDescription }}</el-form-item>
        </el-card>

        <!-- 审核意见 -->
        <el-form-item label="教师意见" v-if="form.status === 1">
          <el-input
            type="textarea"
            v-model="form.teacherOpinion"
            placeholder="请输入教师意见"
          />
        </el-form-item>
        <el-form-item label="学院意见" v-if="form.status === 1">
          <el-input
            type="textarea"
            v-model="form.collegeOpinion"
            placeholder="请输入学院意见"
          />
        </el-form-item>
        <el-form-item label="办公室意见" v-if="form.status === 1">
          <el-input
            type="textarea"
            v-model="form.officeOpinion"
            placeholder="请输入办公室意见"
          />
        </el-form-item>
        <el-form-item label="领导意见" v-if="form.status === 1">
          <el-input
            type="textarea"
            v-model="form.leaderOpinion"
            placeholder="请输入领导意见"
          />
        </el-form-item>
        <el-form-item label="教师意见" v-else>
          <el-input
            type="textarea"
            v-model="form.teacherOpinion"
            disabled
          />
        </el-form-item>
        <el-form-item label="学院意见" v-else>
          <el-input
            type="textarea"
            v-model="form.collegeOpinion"
            disabled
          />
        </el-form-item>
        <el-form-item label="办公室意见" v-else>
          <el-input
            type="textarea"
            v-model="form.officeOpinion"
            disabled
          />
        </el-form-item>
        <el-form-item label="领导意见" v-else>
          <el-input
            type="textarea"
            v-model="form.leaderOpinion"
            disabled
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.status === 1">
        <el-button type="primary" @click="handleApproveSubmit(2)">审核通过</el-button>
        <el-button type="danger" @click="handleApproveSubmit(3)">审核不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getApplicationList,
  getApplicationFromId,
  editApplication
} from "@/api/system/enter-application";

export default {
  name: "EnterApplicationZckj",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请表列表
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        pageSize: 10,
        type: null,
        itemName: null,
        status: null
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询申请表列表 */
    getList() {
      this.loading = true;
      getApplicationList(this.queryParams).then(response => {
        this.applicationList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherOpinion: "",
        collegeOpinion: "",
        officeOpinion: "",
        leaderOpinion: ""
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      getApplicationFromId(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看申请";
      });
    },
    /** 审核按钮操作 */
    handleApprove(row) {
      this.handleView(row);
    },
    /** 提交审核 */
    handleApproveSubmit(status) {
      const data = {
        ...this.form,
        status: status
      };
      editApplication(data).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除该申请？').then(() => {
        return delApplication(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.header {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}
.el-card {
  margin-bottom: 20px;
}
</style>

